using FytSoa.Domain.Car;
using FytSoa.Sugar;
using FytSoa.Common.Utils;
using FytSoa.Domain.Core.Param;
using FytSoa.Domain.Core.Result;
using Mapster;
using Microsoft.AspNetCore.Mvc;
using SqlSugar;

namespace FytSoa.Application.Car;

/// <summary>
/// 服务接口
/// </summary>
[ApiExplorerSettings(GroupName = "v3")]
public class CarsReserveService : IApplicationService
{
    private readonly SugarRepository<CarsReserve> _thisRepository;
    public CarsReserveService(SugarRepository<CarsReserve> thisRepository)
    {
        _thisRepository = thisRepository;
    }
    
    /// <summary>
    /// 查询所有——分页
    /// </summary>
    /// <param name="param"></param>
    /// <returns></returns>
    public async Task<PageResult<CarsReserveDto>> GetPagesAsync(PageParam param)
    {
        var query = await _thisRepository.AsQueryable()
            .WhereIF(!string.IsNullOrEmpty(param.Key),m=>m.Name.Contains(param.Key))
            .ToPageAsync(param.Page, param.Limit);
        return query.Adapt<PageResult<CarsReserveDto>>();
    }

    /// <summary>
    /// 根据主键查询
    /// </summary>
    /// <param name="id"></param>
    /// <returns></returns>
    [HttpGet("{id}")]
    public async Task<CarsReserveDto> GetAsync(long id)
    {
        var model = await _thisRepository.GetByIdAsync(id);
        return model.Adapt<CarsReserveDto>();
    }

    /// <summary>
    /// 添加
    /// </summary>
    /// <param name="model"></param>
    /// <returns></returns>
    public async Task<bool> AddAsync(CarsReserveDto model) =>
        await _thisRepository.InsertAsync(model.Adapt<CarsReserve>());

    /// <summary>
    /// 修改
    /// </summary>
    /// <param name="model"></param>
    /// <returns></returns>
    public async Task<bool> ModifyAsync(CarsReserveDto model) =>
        await _thisRepository.UpdateAsync(model.Adapt<CarsReserve>());

    /// <summary>
    /// 处理查看状态
    /// </summary>
    /// <returns></returns>
    public async Task<bool> ModifyStatusAsync(List<long> ids) =>
        await _thisRepository.UpdateAsync(m => new CarsReserve()
        {
            Status = true
        }, m => ids.Contains(m.Id));

    /// <summary>
    /// 删除,支持多个
    /// </summary>
    /// <param name="ids">逗号分隔</param>
    /// <returns></returns>
    [HttpDelete]
    public async Task<bool> DeleteAsync(string ids) =>
        await _thisRepository.DeleteAsync(m => ids.StrToListLong().Contains(m.Id));
}
